package com.identify.identifydrawings;

import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ImageUploadCut {

    /**
     * The imgur client ID for OkHttp recipes. If you're using imgur for anything other than running
     * these examples, please request your own client ID! https://api.imgur.com/oauth2
     */
    private static final String IMGUR_CLIENT_ID = "123";
    private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
    private static final OkHttpClient client = new OkHttpClient();
    //原是static
    public static void run(final ResponseListener responseListener) throws Exception {
//        Log.e("TAG","runcut="+f);
//        final File file=f;
        new Thread() {
          @Override
            public void run() {
                //子线程需要做的工作
//                RequestBody requestBody = new MultipartBody.Builder()
//                        .setType(MultipartBody.FORM)
//                        .addFormDataPart("title", "Square Logo")
//                        .addFormDataPart("file", UUID.randomUUID().toString()+".png",
//                                RequestBody.create(MEDIA_TYPE_PNG, file))
//                        .build();
                //设置为自己的ip地址
                final Request request = new Request.Builder()
//                        .header("Authorization", "Client-ID " + IMGUR_CLIENT_ID)
                        .url("http://192.168.137.1:5000/uploadcut")//浏览器请求下我看看

                        .get()
                        .build();
                //原代码
//                try (Response response = client.newCall(request).execute()) {
//                    if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
//                    else{
//                        Log.e("TAG", "response=" + response);
//                    System.out.println(response.body().string());}
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
              //修改的代码
              Call call = client.newCall(request);
              call.enqueue(new Callback() {
                  @Override
                  public void onFailure(Call call, IOException e) {
                      Log.e("TAG", "onFailure: " + e.getMessage());

                  }

                  @Override
                  public void onResponse(Call call, Response response) throws IOException {
                      //  String dirName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/LungFile";
                      Log.e("TAG", "responsecut=" + response);
                      //文件夹下有图片
                      String dirName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/cccc";

                      final File file = new File(dirName);
                      if (!file.exists()) {
                          file.mkdir();
                      }

                      File file1 = new File(dirName + "/" + "path.json");
                      if (response != null) {
                          InputStream is = response.body().byteStream();
                          FileOutputStream fos = new FileOutputStream(file1);
                          int len = 0;
                          byte[] buffer = new byte[2048];
                          while (-1 != (len = is.read(buffer))) {
                              fos.write(buffer, 0, len);
                          }
                          fos.flush();
                          fos.close();
                          is.close();
                      }
                      responseListener.onResonse(file1.getAbsolutePath());
                  }
              });
          }
              }.start();
    }
}


